package com.yonyou.sto.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yonyou.sto.common.service.RedisService;
import com.yonyou.sto.modules.ims.model.ImsInvoiceItem;
import com.yonyou.sto.modules.ums.mapper.UmsNcCusttaxMapper;
import com.yonyou.sto.modules.ums.mapper.UmsNcMategradeMapper;
import com.yonyou.sto.modules.ums.model.UmsNcCusttax;
import com.yonyou.sto.modules.ums.model.UmsNcMategrade;
import com.yonyou.sto.modules.ums.service.ConfigNameServic;
import com.yonyou.sto.modules.ums.util.ConfigNameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @文件名: com.yonyou.sto.modules.ums.service.impl
 * @创建用户: MagicJava
 * @项目名: feikai-back
 * @date 2022/12/3 10:25
 */
@Service
public class ConfigNameServicImpl implements ConfigNameServic {
    @Resource
    private RedisService redisService;
    @Resource
    private UmsNcCusttaxMapper umsNcCusttaxMapper;
    @Resource
    private UmsNcMategradeMapper umsNcMategradeMapper;

    /**
     * 以客户NC主键和物料NC主键为key，将客户对账关系对象存入redis
     * 以物料NC主键为key，将物料信息对象存入redis
     */
    @Override
    public void setInitCofigName() {
        QueryWrapper<UmsNcCusttax> ncCusttaxQueryWrapper=new QueryWrapper<>();
        ncCusttaxQueryWrapper.eq("dr", "0");
        List<UmsNcCusttax> umsNcCusttaxes = umsNcCusttaxMapper.selectList(ncCusttaxQueryWrapper);
        umsNcCusttaxes.forEach(umsNcCusttax -> {
            String key=umsNcCusttax.getPkNccust()+umsNcCusttax.getPkNcmateg();
            redisService.set(key,umsNcCusttax);
        });
        QueryWrapper<UmsNcMategrade> ncMategradeQueryWrapper=new QueryWrapper<>();
        ncMategradeQueryWrapper.eq("dr", "0");
        List<UmsNcMategrade> umsNcMategrades=umsNcMategradeMapper.selectList(ncMategradeQueryWrapper);
        umsNcMategrades.forEach(umsNcMategrade -> {
            redisService.set(umsNcMategrade.getPkNc(), umsNcMategrade);
        });
    }

    /**
     * 通过两个NC主键在redis拿对象
     * 物料
     *
     * @param item
     */
    @Override
    public void setCofigName(ImsInvoiceItem item) {
        //
    }

    /**
     * 以客户NC主键和物料NC主键为key，将客户对账关系对象存入redis
     * @param umsNcCusttax
     */
    @Override
    public void setumsNcCusttax(UmsNcCusttax umsNcCusttax) {
        String key=umsNcCusttax.getPkNccust()+umsNcCusttax.getPkNcmateg();
        redisService.set(key,umsNcCusttax);
    }

    public void delumsNcCusttax(UmsNcCusttax umsNcCusttax){
        String key=umsNcCusttax.getPkNccust()+umsNcCusttax.getPkNcmateg();
        redisService.del(key);
    }

    @Override
    public void setumsNcMategrade(UmsNcMategrade umsNcMategrade) {
        redisService.set(umsNcMategrade.getPkNc(), umsNcMategrade);
    }

    @Override
    public UmsNcCusttax getCustCofigName(ImsInvoiceItem item) {
        return umsNcCusttaxMapper.selectOne(new LambdaQueryWrapper<UmsNcCusttax>().eq(UmsNcCusttax::getPkNccust,item.getPkNccust()).eq(UmsNcCusttax::getPkMategrade,item.getPkNcmateg()));
       // return (UmsNcCusttax) redisService.get(item.getPkNccust()+item.getPkNcmateg());
    }

    @Override
    public String getCustCofigStringName(ImsInvoiceItem item) {
        if (item.getPkNcmateg()==null||item.getPkNccust()==null) return item.getCustmatname();
        UmsNcCusttax umsNcCusttax=getCustCofigName(item);//获取配置
        UmsNcMategrade umsNcMategrade= getMateCofigName(item);
        //item.setProductSn(umsNcMategrade.getTaxcode());
        if (umsNcCusttax==null){
//            umsNcMategrade= getMateCofigName(item);
            if (umsNcMategrade==null) return "等待数据同步";
            String custMateName = ConfigNameUtil.getMateItem(umsNcMategrade, item);
            return custMateName;
        }else {
            UmsNcMategrade umsNcMategrade1 = umsNcMategradeMapper.selectOne(new LambdaQueryWrapper<UmsNcMategrade>().eq(UmsNcMategrade::getPkNc,umsNcCusttax.getPkNcmateg()));
            if (umsNcMategrade1==null) return "等待数据同步";
            String custMateName = ConfigNameUtil.getCusttaxItem(umsNcCusttax,umsNcMategrade1, item);
            return custMateName;
        }
    }

    @Override
    public UmsNcMategrade getMateCofigName(ImsInvoiceItem item) {
        return umsNcMategradeMapper.selectOne(new LambdaQueryWrapper<UmsNcMategrade>().eq(UmsNcMategrade::getPkNc,item.getPkNcmateg()));
        //return (UmsNcMategrade) redisService.get(item.getPkNcmateg());
    }

    @Override
    public String getMateCofigStringName(ImsInvoiceItem item) {
        UmsNcCusttax umsNcCusttax=getCustCofigName(item);//获取配置
        UmsNcMategrade umsNcMategrade=null;
        if (umsNcCusttax==null){
            umsNcMategrade= getMateCofigName(item);
            StringBuilder stringBuilder=new StringBuilder();//拼接
            if (umsNcCusttax.getCusttax1().equals('1')){
                stringBuilder.append(umsNcMategrade.getName());
            }

            return null;
        }else {



            return null;
        }


    }

    public String getName(){

        return null;
    }

    @Override
    public String getCustCofigName(UmsNcCusttax umsNcCusttax) {
        UmsNcMategrade umsNcMategrade = umsNcMategradeMapper.selectById(umsNcCusttax.getPkMategrade());
        //UmsNcMategrade umsNcMateg2rade = (UmsNcMategrade)redisService.get(umsNcCusttax.getPkMategrade());
        if (umsNcMategrade==null)return null;
        String configMate = ConfigNameUtil.getConfigMate(umsNcMategrade,umsNcCusttax);
        return configMate;
    }

    @Override
    public String getCusttaxCofigName(UmsNcCusttax umsNcCusttax) {
        String configMate = ConfigNameUtil.getConfigCusttax(umsNcCusttax);
        return configMate;
    }
}
